package org.shilong.UI.OAuth;

import org.shilong.UI.R;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.constants.WeiboConstants;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

public class OAuthFrame extends Activity implements OnClickListener{

	private Weibo mWeibo;

	private Button oauthButton;

	private  String account_name = null;

	private  String account_pwd = null;

	private  String token = null;
	private  String expires_in = null;

	private  String uid = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView( R.layout.layout_oauth_content );

		Bundle bun = getIntent().getExtras();

		account_name = bun.getString( "account_name" );

		account_pwd = bun.getString( "account_pwd" );

		System.out.println( "account_name:"  + account_name );
		System.out.println( "account_pwd:"  + account_pwd );

		initViews();

		registerEvents();
	}

	private void initViews(){

		oauthButton = ( Button )this.findViewById( R.id.to_oauth_button );
	}


	private void registerEvents(){

		oauthButton.setOnClickListener( this );

	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (  v.getId() ) {
		case R.id.to_oauth_button:

			oauth_logic();

			break;

		default:
			break;
		}

	}

	/**
	 * 授权逻辑
	 */
	private void oauth_logic(){


		mWeibo = Weibo.getInstance();

		mWeibo.setupConsumerConfig(WeiboConstants.APIKEY, WeiboConstants.APISECRET);
		mWeibo.setRedirectUrl( WeiboConstants.CALLBACK_URL );

		mWeibo.authorize( this, new AuthDialog() );


	}

	/**
	 * Oauth2 授权Dialog
	 * @author Administrator
	 *
	 */
	private class AuthDialog  implements WeiboDialogListener{

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onComplete(Bundle values) {


			token = values.getString("access_token");
			expires_in = values.getString("expires_in");

			System.out.println("获取的token:" + token );
			System.out.println("获取的expires_in:" + expires_in);

			AccessToken accessToken = new AccessToken(token, WeiboConstants.APISECRET);
			accessToken.setExpiresIn(expires_in);

			Weibo.getInstance().setAccessToken(accessToken);

			System.out.println("授权成功...");

			Bundle bun = new Bundle();
			
			bun.putString( "a_name", account_name );
			bun.putString( "a_pwd", account_pwd );
			bun.putString( "a_token", token );
			bun.putString( "a_expires_in", expires_in);
			
			IntentUtility.goNextFrame( OAuthFrame.this, OAuth2Frame.class, bun );
			
			OAuthFrame.this.finish();

		}

		@Override
		public void onError(DialogError e) {
			// TODO Auto-generated method stub
			System.out.println( "onError");
		}

		@Override
		public void onWeiboException(WeiboException e) {
			// TODO Auto-generated method stub
			System.out.println( "WeiboException");
		}
	}

	

}
